package com.mloeppky.setgame.client;

import static com.google.common.collect.Lists.newArrayList;

import java.util.List;

import com.google.gwt.event.dom.client.FocusHandler;
import com.mloeppky.setgame.client.SetCard.Color;
import com.mloeppky.setgame.client.SetCard.Fill;
import com.mloeppky.setgame.client.SetCard.Shape;

/**
 * A deck of {@link SetCards}. A standard deck in set contains 81 cards.
 * @author mloeppky
 *
 */
public class SetDeck {
	/**
	 * The cards in the deck
	 */
	private List<SetCard> cards;
	
	/**
	 * Generates a {@link SetDeck} of all 81 {@link SetCard}s
	 * @param handler The class to receive {@link FocusEvents} from the {@link SetCard}s
	 * in the deck.
	 */
	public SetDeck(FocusHandler handler) {
		cards = newArrayList();
		for (int qty = 1; qty <= 3; qty++) {
			for (Color color : Color.values()) {
				for (Shape shape : Shape.values()) {
					for (Fill fill : Fill.values()) {
						SetCard sc = new SetCard(qty, color, shape, fill);
						sc.addFocusHandler(handler);
						cards.add(sc);
					}
				}
			}
		}
	}
	
	/**
	 * Draws a card
	 * @return A {@link SetCard} drawn from the deck.
	 * @throws EmptyDeckException If all 81 {@link SetCard}s have been drawn from the deck.
	 */
	public SetCard drawCard() throws EmptyDeckException {
		if (cards.isEmpty()) {
			throw new EmptyDeckException();
		} else {
			int i = (int) (cards.size() * Math.random());
			SetCard sc = cards.remove(i);
			return sc;
		}
	}
}
